"""
测试Image_Utils功能
"""

import os
import tempfile
from com.tools.Image_Utils import ImageUtils


def test_image_utils():
    """
    测试图片工具类
    """
    # 创建临时目录用于测试
    with tempfile.TemporaryDirectory() as temp_dir:
        print(f"使用临时目录进行测试: {temp_dir}")
        
        # 测试创建空白图片
        print("\n测试创建空白图片:")
        img = ImageUtils.create_blank_image((400, 300), color="white")
        blank_image_path = os.path.join(temp_dir, "blank.jpg")
        img.save_image(blank_image_path)
        print(f"创建空白图片: {blank_image_path}")
        
        # 测试获取图片信息
        print("\n测试获取图片信息:")
        info = img.get_image_info()
        print(f"图片信息: {info}")
        assert info['size'] == (400, 300)
        assert info['mode'] == 'RGB'
        
        # 测试添加文字
        print("\n测试添加文字:")
        img.add_text("Hello World", (50, 50), font_size=30, color="black")
        text_image_path = os.path.join(temp_dir, "with_text.jpg")
        img.save_image(text_image_path)
        print(f"添加文字后保存: {text_image_path}")
        
        # 测试添加图形
        print("\n测试添加图形:")
        img.add_rectangle((100, 100, 200, 200), outline="red", width=3)
        img.add_ellipse((250, 100, 350, 200), outline="blue", width=2, fill="yellow")
        img.add_line((50, 250, 350, 250), fill="green", width=5)
        shapes_image_path = os.path.join(temp_dir, "with_shapes.jpg")
        img.save_image(shapes_image_path)
        print(f"添加图形后保存: {shapes_image_path}")
        
        # 测试图片调整
        print("\n测试图片调整:")
        # 调整亮度
        img.adjust_brightness(1.2)
        brightness_image_path = os.path.join(temp_dir, "brightness.jpg")
        img.save_image(brightness_image_path)
        print(f"调整亮度后保存: {brightness_image_path}")
        
        # 调整对比度
        img.adjust_contrast(1.1)
        contrast_image_path = os.path.join(temp_dir, "contrast.jpg")
        img.save_image(contrast_image_path)
        print(f"调整对比度后保存: {contrast_image_path}")
        
        # 测试图片旋转
        print("\n测试图片旋转:")
        img.rotate_center(45)
        rotated_image_path = os.path.join(temp_dir, "rotated.jpg")
        img.save_image(rotated_image_path)
        print(f"旋转后保存: {rotated_image_path}")
        
        # 测试图片翻转
        print("\n测试图片翻转:")
        # 创建新图片进行翻转测试
        img_flip = ImageUtils.create_blank_image((200, 200), color="lightblue")
        img_flip.add_text("Flip Test", (50, 90), font_size=20, color="black")
        
        # 水平翻转
        img_flip.flip_horizontal()
        flip_horizontal_path = os.path.join(temp_dir, "flip_horizontal.jpg")
        img_flip.save_image(flip_horizontal_path)
        print(f"水平翻转后保存: {flip_horizontal_path}")
        
        # 垂直翻转
        img_flip2 = ImageUtils.create_blank_image((200, 200), color="lightgreen")
        img_flip2.add_text("Flip Test", (50, 90), font_size=20, color="black")
        img_flip2.flip_vertical()
        flip_vertical_path = os.path.join(temp_dir, "flip_vertical.jpg")
        img_flip2.save_image(flip_vertical_path)
        print(f"垂直翻转后保存: {flip_vertical_path}")
        
        # 测试图片模式转换
        print("\n测试图片模式转换:")
        img_mode = ImageUtils.create_blank_image((100, 100), color="red")
        img_mode.convert_mode("L")  # 转换为灰度图
        grayscale_path = os.path.join(temp_dir, "grayscale.jpg")
        img_mode.save_image(grayscale_path)
        print(f"灰度图保存: {grayscale_path}")
        
        # 验证模式转换
        info = img_mode.get_image_info()
        assert info['mode'] == 'L'
        
        print("\n所有测试通过!")


if __name__ == "__main__":
    test_image_utils()